/*
 * Copyright (C), 2005-2019, 深圳市珍爱网信息技术有限公司
 */

package org.idea.ietty.container;

import com.sun.scenario.effect.impl.prism.PrImage;
import org.idea.ietty.common.annotation.Resource;
import org.idea.ietty.common.annotation.RestController;
import org.idea.ietty.common.util.ClassUtil;
import org.idea.ietty.container.bean.TestController;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @author linhao
 * @date 2020/5/7
 * @Version V1.0
 */
public class XmlBeanConfigParser implements BeanConfigParser{

    private IettyApplicationContext iettyApplicationContext;

    public void setIettyApplicationContext(IettyApplicationContext iettyApplicationContext) {
        this.iettyApplicationContext = iettyApplicationContext;
    }

    @Override
    public List<Object> parse(InputStream inputStream) {
        List beanDefinitions = new ArrayList<>();
        try {
            DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
            Document doc = documentBuilder.parse(inputStream);
            doc.getDocumentElement().normalize();
            // 加载常规的bean
            NodeList beanList = doc.getElementsByTagName("bean");
            for (int i=0;i<beanList.getLength();i++) {
                Node node = beanList.item(i);
                if (node.getNodeType() != Node.ELEMENT_NODE){
                    continue;
                }
                Element element = (Element) node;
                BeanDefinition beanDefinition = new BeanDefinition(
                        element.getAttribute("id"),
                        element.getAttribute("class"),
                        BeanDefinition.DefinitionType.XML
                );
                // 加载常规的bean
                beanDefinitions.add(beanDefinition);
            }

            //加载一些使用注解声明的bean
            NodeList contextScan = doc.getElementsByTagName("context-scan");
            if(contextScan.getLength()!=1){
                throw new RuntimeException("only need one context-scan-setting");
            }
            Node node = contextScan.item(0);
            Element element = (Element) node;

            AnnotationBeanDefinition contextLoadConfig = new AnnotationBeanDefinition(
                    element.getAttribute("baseScanPackage"));
            String scanLocation = contextLoadConfig.getScanLocation();
            if(scanLocation.endsWith("*")){
                scanLocation =scanLocation.substring(0,scanLocation.lastIndexOf("."));
            }
            Set<Class<?>> classSet = ClassUtil.getClassFromPkg(scanLocation);
            for (Class<?> aClass : classSet) {
                if(aClass.isAnnotationPresent(RestController.class)){
                    RestController restController = aClass.getAnnotation(RestController.class);
                    BeanDefinition beanDefinition = new BeanDefinition(
//                            aClass.getSimpleName().substring(0,1).toLowerCase()+aClass.getSimpleName().substring(1),
                            restController.url(),
                            aClass.getName(),
                            BeanDefinition.DefinitionType.ANNOTATION
                    );
                    beanDefinitions.add(beanDefinition);
                }
            }


        } catch (ParserConfigurationException | SAXException | IOException e) {
            e.printStackTrace();
        }
        return beanDefinitions;
    }

    public static void main(String[] args) {
        TestController testController = new TestController();
        String name = testController.getClass().getSimpleName();
        System.out.println();
    }


}
